package cn.stylefeng.guns.modular.bi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.bi.entity.KnowledgeBase;
import cn.stylefeng.guns.modular.bi.model.params.KnowledgeBaseParam;
import cn.stylefeng.guns.modular.bi.service.KnowledgeBaseService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *
 *
 * @Author weiqixuan
 * @Date 2025/8/22 14:29
 */
@Controller
@RequestMapping("/knowledgeBase")
public class KnowledgeBaseController extends BaseController {

    private String PREFIX = "/bi/knowledgeBase";

    @Autowired
    private KnowledgeBaseService knowledgeBaseService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-07-30
     */
    @RequestMapping("/index")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/knowledgeBase.html";
    }
    // 保留原有的空路径映射
    @RequestMapping("")
    public String indexEmpty(Model model) {
        return index(model);
    }


    /**
     * 新增页面
     * @return
     */
    @RequestMapping("/addPage")
    public String add(){ return PREFIX + "/knowledgeBase_add.html";}

    /**
     * 修改页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){ return PREFIX + "/knowledgeBase_edit.html";}


/**
 * 详情页面
 * @return
 */
@RequestMapping("/detailPage")
public String detailPage(Long id, Model model) {
    // 将id传递到页面，以便页面加载时获取详情数据
    if (id != null) {
        model.addAttribute("id", id);
    }
    return PREFIX + "/knowledgeBase_detail.html";
}

/**
 * 编辑页面
 * @return
 */
@RequestMapping("/editPage")
public String editPage(Long id, Model model) {
    // 将id传递到页面，以便页面加载时获取编辑数据
    if (id != null) {
        model.addAttribute("id", id);
    }
    return PREFIX + "/knowledgeBase_edit.html";
}
    /**
     * 查询实施问题知识库列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(KnowledgeBaseParam knowledgeBaseParam) {
        return this.knowledgeBaseService.findPageBySpec(knowledgeBaseParam);
    }

    /**
     * 添加实施问题知识库
     */
    @ResponseBody
    @RequestMapping("/add")
    public ResponseData add(KnowledgeBaseParam knowledgeBaseParam) {
        this.knowledgeBaseService.add(knowledgeBaseParam);
        return ResponseData.success();
    }

    /**
     * 编辑实施问题知识库
     */
    @ResponseBody
    @RequestMapping("/edit1")
    public ResponseData edit(KnowledgeBaseParam knowledgeBaseParam) {
        this.knowledgeBaseService.update(knowledgeBaseParam);
        return ResponseData.success();
    }

    /**
     * 删除实施问题知识库
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseData delete(KnowledgeBaseParam knowledgeBaseParam) {
        this.knowledgeBaseService.delete(knowledgeBaseParam);
        return ResponseData.success();
    }

    /**
     * 小程序端根据标题模糊搜索
     */
    @ResponseBody
    @RequestMapping("/search")
    public ResponseData search(String title) {
        List<KnowledgeBase> list = this.knowledgeBaseService.searchByTitle(title);
        return ResponseData.success(list);
    }

    /**
     * 获取实施问题知识库详情
     */
    @ResponseBody
    @RequestMapping("/detail")
    public ResponseData detail(Long id) {
        KnowledgeBase knowledgeBase = this.knowledgeBaseService.getById(id);
        return ResponseData.success(knowledgeBase);
    }

}
